var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //下拉的label文字
    areas_label: '区域',
    price_label: '总价',
    sufix_label: '面积',
    room_label: '户型',
    //搜索框隐藏
    keyboard: 1,
    flage: false,
    //搜索字段
    fid: 0,
    keyword: '',
    areas: '',
    min_price: 0,
    max_price: 0,
    min_sufix: 0,
    max_sufix: 0,
    room: 0,
    rent_type: 0,
    order: 0,
    zhutype: 0,
    //当前选中的下拉项
    currentIndex: 0,
    //搜索下拉选项

    /**更多搜索 */
    sboxli: ['不限制', '不限制', '不限制', '不限制'],
    sboxmore: ['0', '0', '0', '0', '0', '0'],
    //房源列表
    houselist: {},
    page: 1,
    islast: false,
    isjingping: 0,
    istotal: false,
    /**置顶房源 */
    tophouse: [],
    /**弹窗显示 */
    hiddendialog: true,
    positon: "", /**弹窗的位置样式 */
  },
  /**处理全局事件统一处理 */
  onDefRedirect: function (e) {
    app.kbtools.defRedirect(this, e);
  },
  /**弹窗控制 */
  trigger_dialog: function (e) {
    console.log(e);
    var index = e.currentTarget.dataset.name;

    var dialog = {
      'shownav': {
        title: '请选择想要查找的房源类型',
        items: this.data.searchdata.house_nav_block
      }

    }
    var isshow = this.data.hiddendialog;
    this.setData({
      hiddendialog: isshow ? false : true,
      positon: "top: 300rpx",
      dialog: dialog[index]
    })
  },
  /**
   * 切换搜索条件的下拉
   */
  chagetab: function (e) {
    const data = e.currentTarget.dataset;
    const self = this;
    if (self.data.currentIndex == data.index) {
      data.index = 0;
    }
    self.setData({ "currentIndex": data.index });

  },
  /**
   * 点击搜索下拉项目的事件
   */
  /**更多搜索充值 */
  search_nav_rest: function (e) {
    this.setData({ sboxmore: ['0', '0', '0', '0', '0', '0'] })
  },
  search_nav: function (e) {
    var data = e.currentTarget.dataset;
    var label = data.label;
    var show = { areas: '区域', price: '总价', sufix: '面积', room: '户型' };
    if (data.label == '不限制') {
      label = show[data.index];
    }
    if (data.type == 1) { //价格 和户型区间
      var tt = [];
      if (data.txt.indexOf('-') > 0) {
        tt = data.txt.split("-");
      } else {
        tt[0] = tt[1] = 0;
      }

    } else if (data.type == 0) { //区域 户型
      var tt = data.txt;
    }
    if (data.index == 'price') {
      this.setData({ min_price: tt[0], max_price: tt[1], price_label: label });
    } else if (data.index == 'sufix') {
      this.setData({ min_sufix: tt[0], max_sufix: tt[1], sufix_label: label });
    } else if (data.index == 'areas') {
      this.setData({ areas: tt, areas_label: label });
    } else if (data.index == 'room') {
      this.setData({ room: tt, room_label: label });
    } else if (data.index == 'keyword') {
      this.setData({ keyword: e.detail.value });
    } else if (data.index == 'order') {
      this.setData({ order: tt });
    }
    /**初始化搜索条件 */
    /**当前操作项目 */
    var sboxli = this.data.sboxli;
    var curr = this.data.currentIndex;
    sboxli[curr] = data.txt;
    this.setData({
      "currentIndex": 0,
      "houselist": [], "page": 1,
      "islast": false, "istotal": false,
      sboxli: sboxli
    });
    /**置顶房源 */
   // this.search_house_top();
    /**非置顶房源 */
    this.search_house(0);
  },
  /**
   * 获取搜索的参数
   */
  search_params: function () {
    const self = this
    app.util.request({
      'url': 'entry/wxapp/thread',
      'cachetime': '30',
      data: { 'enews': 'searchkey', "fid": self.data.fid },
      success(res) {
        var searchdata = res.data.data;
        /**搜索参数 */
        var nav = {
          flextab: [
            { label: '区域', index: 1 },
            { label: searchdata.price_label , index: 2 },
            { label: '面积', index: 3 } 
          ],
          flexcon: [
            {
              showkey: 1,
              sboxtitle: [
                { label: '按区域搜', sort: 1 },
                { label: '在地图搜', sort: 2, url: '../map/index?tags=0' }
              ],
              flexli: [
                {
                  index: 'areas',
                  label: '不限制',
                  keytype: 0,
                  show: '不限制',
                  select: searchdata.areas
                }
              ]
            },
            {
              showkey: 2,
              sboxtitle: [
                { label: searchdata.price_label , sort: 2 }
              ],
              flexli: [
                {
                  index: 'price',
                  label: '不限制',
                  keytype: 1, /**1= 数字，0=文本 */
                  show: '不限制',
                  select: searchdata.price
                }
              ]
            },
            {
              showkey: 3,
              sboxtitle: [
                { label: '面积', sort: 3 }
              ],
              flexli: [
                {
                  index: 'sufix',
                  label: '不限制',
                  keytype: 1, /**1= 数字，0=文本 */
                  show: '不限制',
                  select: searchdata.sufix
                }
              ]
            },
          ],
          /**更多类型下拉 */
          sboxmore:  [ ],
          /**搜索排序 */
          search_order: searchdata.secorder
        }
        /**搜索参数 */
        self.setData({ flextab: nav, searchdata: searchdata });
      }
    })
  },
  changeBtnStatus: function (e) {
    var that = this;
    var data = e.currentTarget.dataset;
    var sboxmore = that.data.sboxmore;
    if (data.index == 'room') {
      sboxmore[0] = (sboxmore[0] == data.txt) ? "" : data.txt;
    }
    if (data.index == 'housetype') {
      sboxmore[1] = (sboxmore[1] == data.txt) ? "" : data.txt;
    }
    if (data.index == 'zhaungxiu') {
      sboxmore[2] = (sboxmore[2] == data.txt) ? "" : data.txt;
    }
    if (data.index == 'houseyears') {
      sboxmore[3] = (sboxmore[3] == data.txt) ? "" : data.txt;
    }
    if (data.index == 'ptype') {
      sboxmore[4] = (sboxmore[4] == data.txt) ? "" : data.txt;
    }

    that.setData({
      sboxmore: sboxmore
    });

  },
  /**
   * 获取查询字段
   */
  house_search_field: function (self, istop) {
    var params = {
      area: self.data.areas,
      minprice: self.data.min_price,
      maxprice: self.data.max_price,
      minsufix: self.data.min_sufix,
      maxsufix: self.data.max_sufix,
      room: self.data.room,
      keyword: self.data.keyword, 
      order: self.data.order,
      fid: self.data.fid
    }
    if (istop == 1) {
      params.istop = 1;
      params.page = 0;
    } else {
      params.istop = 0;
      params.page = self.data.page;
    }
    return params;
  },
  /**
   * 发送查询参数 ，返回结果了列表
   */
  /**置顶房源 */
  search_house_top: function () {
    const self = this;
    var params = this.house_search_field(this, 1);
    app.util.request({
      'url': 'entry/wxapp/thread',
      'cachetime': '0',
      showLoading: true,
      data: params,
      success(res) {

        self.setData({
          tophouse: res.data.data
        });
      }
    });
  },
  /**房源列表 */
  search_house: function () {
    const self = this;
    self.setData({ islast: true });
    var params = this.house_search_field(this, 0);
    app.util.request({
      'url': 'entry/wxapp/thread',
      'cachetime': '0',
      showLoading: true,
      data: params,
      success(res) {
        /**如果还有数据可以继续分页查询更多 */
        if (!app.kbtools.isnull(res.data.data)) {
          if (res.data.data.length > 0) {
            self.setData({ istotal: false });;
          }
        } else {
          self.setData({ istotal: true });
        }

        /**处理数据 */
        var house = self.data.houselist;
        if (self.data.page > 1) {
          house = house.concat(res.data.data);
        } else {
          house = res.data.data;
        }
        self.setData({
          houselist: house
        });

      }
    })
  },
  /**
   * 搜索框失去焦点 改变keyword
   */
  search_keyword: function (e) {
    this.setData({ keyword: e.detail.value });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    const self = this;

    if (!app.kbtools.isnull(options.id)) {
      this.setData({
        fid: options.id
      });
    }
    /**更多条件的搜索 morefind=0-0-0-0-1-0  表示整租*/
    if (!app.kbtools.isnull(options.morefind)) {
      var sboxmore = options.morefind;
      sboxmore = sboxmore.split("-");
      this.setData({
        sboxmore: sboxmore
      });
    }
    /**关键词搜索 */
    if (!app.kbtools.isnull(options.keyword)) {
      this.setData({
        keyword: options.keyword
      })
    }

    // 页面渲染完成  
    self.search_params();
    
    self.search_house(0);
    /**广告数据 */
    app.kbtools.siteAdvancise(app, self);
  },
  /**
   * 页面滚动事件
   */
  onPageScroll: function (e) {

    var now = this.data.keyboard;
    var flag = this.data.flage;
    if (e.scrollTop > 20) {
      if (!flag) {
        this.setData({
          keyboard: 0,
          flage: true
        })
      }
    } else {
      if (flag) {
        this.setData({
          keyboard: 1,
          flage: false
        })
      }
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    const self = this;
   
    wx.getStorage({
      key: 'shop',
      success: function (res) {
        wx.setNavigationBarTitle({
          title: res.data.shopname ,
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.search_house_top();
    this.search_house(0);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const that = this;
    if (!that.data.istotal) {
      this.setData({ page: that.data.page + 1 });
      that.search_house(0);
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})